﻿using System;
using System.Collections;
using System.Web;
using BONSAI.BLL;
using BONSAI.Common;
using System.Linq;

namespace BONSAI.UI.Pages
{
    public partial class HomeDetail : System.Web.UI.Page
    {
        public string ProductId { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ProductId = HttpContext.Current.Items[BONSAIConstants.PRODUCTID] + "";
                BindProduct(ProductId);
                lblId.Text = ProductId;
            }
        }
        private void BindImageofProdcut(string id)
        {
            var lst = (new PhotosBO()).GetAll().Where(x => x.ProductsID == id);
            if (lst.Count() > 0)
            {
                var str = string.Empty;
                str += "<div class=\"dt-other-left\"><img src=\"../Images/cursor-sub-menu-left.png\" alt=\"\" class=\"prev1\" style=\"padding-top: 35px;\" /></div>";
                str += "<div class=\"dt-other-center  jCarouselLite1\">";
                str += "<ul>";
                foreach (var s in lst)
                {
                    string urlImg = s.thumbnail;
                    if (string.IsNullOrEmpty(s.thumbnail))
                    {
                        urlImg = "~/Images/no-image.jpg";
                    }
                    str += "<li><img src=\"" + urlImg + "\" alt=\"\" width=\"70\" height=\"70\" /></li>";
                }
                str += "</ul>";
                str += "</div>";
                str += "<div class=\"dt-other-right\"><img src=\"../Images/cursor-sub-menu.png\" alt=\"\" class=\"next1\" style=\"padding-top: 35px;\" /></div>";
                ltrImage.Text = str;
            }
        }

        private void BindProduct(string id)
        {
            var lst = (new ProductsBo()).GetById(id);
            if (lst == null) return;
            BindImageofProdcut(id);
            //load orther project 
            LoadOtherProducts(lst.TypeProductsID, id);

            lblDate.Text = lst.Createdate.ToString("dd/MM/yyyy hh:mm:ss tt");
            lblName.Text = lst.NameFull;
            lblCode.Text = lst.ProductsCode;
            string urlImg = lst.thumbnail;
            if (string.IsNullOrEmpty(lst.thumbnail))
            {
                urlImg = "~/Images/no-image.jpg";
            }
            string urlImgPre = lst.thumbnail;
            if (string.IsNullOrEmpty(lst.preview))
            {
                urlImgPre = "~/Images/no-image.jpg";
            }
            imgthubnail.ImageUrl = urlImg;
            lnkZoom.NavigateUrl = urlImgPre;
            lblId1.Text = lst.NameFull;
            ltrDescription.Text = lst.Description;

            if (string.IsNullOrEmpty(lst.Pricenew.ToString()))
            {
                lblPrice.Text =
                    "<span class=\"span1\">" + lst.Pricenew.ToString("#.###") +
                    "</span><span class=\"span2\">VNĐ</span><span class=\"span3\">" + lst.PriceOld.ToString("#.###") +
                    " VNĐ</span>";
            }
            else
            {
                lblPrice.Text =
                    "<span class=\"span1\">" + lst.PriceOld.ToString("#.###") +
                    "</span><span class=\"span2\">VNĐ</span>";
            }
        }

        private void LoadOtherProducts(string typeId, string id)
        {
            product_order.Visible = true;
            var lst = (new ProductsBo()).GetAll().Where(x => x.TypeProductsID == typeId && x.ProductsID != id);
            if (lst.Count() < 1)
            {
                product_order.Visible = false;
                return;

            }
            foreach (var item in lst)
            {
                if (string.IsNullOrEmpty(item.thumbnail))
                {
                    item.thumbnail = "~/Images/no-image.jpg";
                }
            }
            Repeater1.DataSource = lst;
            Repeater1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(lblId.Text))
            {
                var lst = (new ProductsBo()).GetById(lblId.Text);
                if (lst == null) return;
                var cart = new ShoppingCart();
                if (Session[BONSAIConstants.SHOPPING_CART] != null)
                {
                    cart.gCart = (ArrayList)Session[BONSAIConstants.SHOPPING_CART];
                }
                cart.addCart(lst.ProductsCode, lst.NameFull, 1,
                                     Math.Abs(lst.Pricenew - 0) < 1 ? lst.PriceOld : lst.Pricenew, lst.thumbnail);
                Session[BONSAIConstants.SHOPPING_CART] = cart.gCart;
                Response.Redirect("~/home-cart/");
            }
        }
    }
}